package com.hb.trade.server.flow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.flow.entity.FlowTempForm;
import com.hb.core.pojo.flow.entity.FlowTempFormField;
import com.hb.core.pojo.flow.dto.FlowTempFormDTO;
import com.hb.core.pojo.parent.MPBaseController;
import com.hb.trade.server.flow.service.FlowTempFormFieldService;
import com.hb.trade.server.flow.service.FlowTempFormService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 流程表单信息(模板) 前端控制器
 * </p>
 *
 * @author zhaojk
 * @since 2021-05-27
 */
@RestController
@RequestMapping("/flowTempForm")
public class FlowTempFormController extends MPBaseController<FlowTempForm, FlowTempFormService> {

    protected final FlowTempFormFieldService flowTempFormFieldService;

    public FlowTempFormController(FlowTempFormService service,
                                  FlowTempFormFieldService flowTempFormFieldService) {
        super(service);
        this.flowTempFormFieldService = flowTempFormFieldService;
    }

    /**
     * 获取所有表单类型
     *
     * @return
     */
    @PostMapping("/flowTemps")
    public Message flowTemps() {
        QueryWrapper<FlowTempForm> wrapper =
                FlowTempForm.wrapper("id,form_name,form_type,form_note");
        wrapper.orderByDesc("create_time");
        List<FlowTempForm> forms = service.list(wrapper);
        return MsgUtil.success(forms);
    }

    /**
     * 获取指定表单的字段信息
     *
     * @param formId
     * @return
     */
    @PostMapping("/tempFormFields")
    public Message tempFormFields(Long formId) {
        QueryWrapper<FlowTempFormField> wrapper =
                FlowTempFormField.wrapper("`id`,`form_id`,`temp_main_id`," +
                        "`field_code`,`field_name`,`field_type`,`not_null_flag`, " +
                        "`sort_num`, `delete_flag`");
        wrapper.eq("form_id", formId).orderByAsc("sort_num");
        List<FlowTempFormField> formFields = flowTempFormFieldService.list(wrapper);
        return MsgUtil.success(formFields);
    }

    /**
     * 新增表单信息
     *
     * @param formDto
     * @return
     */
    @PostMapping("/newForm")
    public Message newForm(FlowTempFormDTO formDto) {
        boolean r = service.saveFlowTempForm(formDto);
        return MsgUtil.success(r);
    }
}
